<?php

/**
 * default Components.
 *
 * @package    social
 * @subpackage default
 * @author     Your name here
 * @version    SVN: $Id: Components.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class wynikiComponents extends sfComponents
{
  
    /**
   * Zwraca różnice pomiedzy pierwsza a ostatnim wynikiem dla danego id_rozgrywki
   * @param sfWebRequest $request 
   */
  public function executeGetAllDatesInARow(sfWebRequest $request)
  {  
      
    $userid = $this->getUser()->getGuardUser()->getId();
    
   // if(null == $this->result_id):
        $game_results = Doctrine_Core::getTable('GameResult')
          ->createQuery('a')
          ->where('a.id = ?', $this->result_id)
          ->execute();
    
   // else:
   /* 
    $game_results = Doctrine_Core::getTable('GameResult')
      ->createQuery('a')
      ->where('a.user_id = ?', $userid)
      ->addWhere('a.games_id = ?', $this->gameid)
      ->addWhere('a.rozgrywka_id = ?', $this->rozgrywka_id)
      ->orderBy('a.end_date ASC')
      ->execute();*/
    
  //  endif;
    
    $this->dates = array();
    foreach ($game_results as $result):
        $this->dates[] = array('start' => $result->start_date, 'end' => $result->end_date);
    endforeach;
    
    
    
  }
  
  public function executeGrupaWynikiOgolnePodopiecznego(sfWebRequest $request)
  {  
    
    $query = Doctrine_Query::create()
      ->from('GameResult a')
      ->select("a.*, DATEDIFF(NOW(), a.end_date) AS ile_dni, COUNT(a.score) as ilosc, MAX(a.score) as max_pkt, AVG(a.score) as avg_pkt")
      ->where('a.user_id = ?', $this->memberid )
      ->orderBy('a.end_date DESC')
      ->addGroupBy('a.rozgrywka_id');   
    
    $this->game_results = new sfDoctrinePager(
        'GameResults',
        sfConfig::get('app_max_game_result_on_page')
    );
    $this->game_results->setQuery($query);
    $this->game_results->setPage($request->getParameter('page', 1));
    $this->game_results->init();   
    
  }

  public function executeGrupaWynikiPodopiecznego(sfWebRequest $request)
  {  
    
    $query = Doctrine_Query::create()
      ->from('GameResult a')
      ->select("a.*, DATEDIFF(NOW(), a.end_date) AS ile_dni, COUNT(a.score) as ilosc, MAX(a.score) as max_pkt, AVG(a.score) as avg_pkt")
      ->where('a.user_id = ?', $this->memberid )
      ->orderBy('a.end_date DESC')
      ->addGroupBy('a.rozgrywka_id');   
    
    $this->game_results = new sfDoctrinePager(
        'GameResults',
        sfConfig::get('app_max_game_result_on_page')
    );
    $this->game_results->setQuery($query);
    $this->game_results->setPage($request->getParameter('page', 1));
    $this->game_results->init();   
    
  }

    
}